/*
Table_A01. Productivity and sentiment: Additional lagged controls
Table_A02. Productivity and sentiment: Extended sample
Table_A03. Macroeconomic uncertanty and G7 countries
Table_A04. Productivity and sentiment: Alternative country breakdown
Table_A05. Productivity and sentiment: Extended sample breakdown
Table_A06. Productivity and sentiment: Alternative FD measures
Table_A07. Productivity and sentiment: FD and quality of institutions
Table_A08. Investment in R&D
Table_A09. Capital intensity
Table_A10. Future stock returns, sentiment, and local macro variables
Table_A11. Future stock returns: High v. low sentiment breakdown
Table_A12. Market efficiency and G7 countries
Table_A13. Bond returns
Table_A14. Macroeconomic forecasts
Table_A15. Foreign direct investments
*/



* Table_A01. Productivity and sentiment: Additional lagged controls
	clear all
	use "data/final_data_set.dta"
	xtset id year
	
	// generate lagged dependent and independent variables
		foreach x in zcci zd_rgdpna_1tr zd_emp zd_rconna_1tr zd_labsh zd_pl_con ///
		zF1 zF2 zF3 zF4 zF5 zF6 zF7 zF8 rtfpna ///
		{
			gen l_`x' = l.`x'
			gen l2_`x' = l2.`x'
		}
	// create macros
		global controls1 l_zcci l_zd_* l_zF* l_rtfpna
		global controls2 l2_zcci l2_zd_* l2_zF* l2_rtfpna

	// regressions	
		foreach x in f_rtfpna f2_rtfpna f3_rtfpna f4_rtfpna {
			qui: regxfe `x' zcci zd_* zF* $controls1 $controls2, robust fe(id) cluster(year)
				eststo
		}
		esttab using "Table_A01.rtf", replace cells(b(star fmt(%9.4f)) t(fmt(%9.2f))) style(fixed) title(Table_TBD) ///
		t starlevels(* .10 ** .05 *** .01) b(4) scalars(r2o)     ///
		drop()
		estimates clear

	
* Table_A02: Productivity and sentiment: Extended sample
	clear all
	use "data/final_data_set_extended.dta"
	xtset id year

	// country groups
		gen dum_nonG7_main = (cou == "AUS" | cou == "AUT" | cou == "BEL" | cou == "CHE" | cou == "DNK" | cou == "FIN" | cou == "IRL" | cou == "ESP" | cou == "NLD" | cou == "NZL" | cou == "SWE")	
		tabulate country if dum_G7 == 1
		tabulate country if dum_G7 == 0
		tabulate country if dum_nonG7_main == 1
		tabulate country if dum_G7 == 0 & dum_nonG7_main == 0
		tabulate country if zcci != . & rtfpna != .
	
	// regressions
		foreach x in f_rtfpna f2_rtfpna f3_rtfpna f4_rtfpna {
			qui: regxfe `x' zcci zd_* zF*, robust fe(id) cluster(year)
				eststo
		}
		esttab using "Table_A02.rtf", replace cells(b(star fmt(%9.4f)) t(fmt(%9.2f))) style(fixed) title(Table_A02) ///
		t starlevels(* .10 ** .05 *** .01) b(4) scalars(r2o)     ///
		drop()
		estimates clear

		
* Table_A03: Macroeconomic uncertanty and G7 countries
	// annual
		clear all
		use "data/uncertainty.dta"
		egen id = group(cou)
		xtset id year

		// dummy for G7 countries (USA, CAN, FRA, DEU, ITA, JPN, GBR)
			// including the U.S.
				gen dum_G7 = (cou == "USA" | cou == "CAN" | cou == "FRA" | cou == "DEU" | cou == "ITA" | cou == "JPN" | cou == "GBR")
			// excluding the U.S.
				gen dum_G7n = (cou == "CAN" | cou == "FRA" | cou == "DEU" | cou == "ITA" | cou == "JPN" | cou == "GBR")
			// excluding Japan
				gen dum_G7j = (cou == "USA" | cou == "CAN" | cou == "FRA" | cou == "DEU" | cou == "ITA" | cou == "GBR")

		// dummy for non-G7 countries (AUS, CHE, ESP, IRL, NLD, NZL, SWE)
				gen dum_nonG7 = (cou == "AUS" | cou == "CHE" | cou == "ESP" | cou == "IRL" | cou == "NLD" | cou == "NZL" | cou == "SWE")

		// dummy for countries in our sample
				gen sample = dum_G7j + dum_nonG7
				
		keep if sample == 1		
		keep if year < 2020
			// ssc install asdoc
			// help asdoc

		/* 
			bys cou : asdoc sum unc_tot, stat(mean p50 sd N) format(%9.4f) replace
			bys cou : asdoc sum unc_id, stat(mean p50 sd N) format(%9.4f) replace
			bys cou : asdoc sum unc_com, stat(mean p50 sd N) format(%9.4f) replace
			bys cou : asdoc sum epu, stat(mean p50 sd N) format(%9.4f) replace

			asdoc tabstat unc_tot, statistics(p50) by(cou) columns(variables)
			asdoc tabstat epu, statistics(p50) by(cou) columns(variables)
		*/
			
		// take logs
			foreach x in unc_tot unc_com unc_id {
				gen ln_`x' = ln(`x')
			}
				
		// standardize and winsorize variables
			foreach x in unc_tot unc_com unc_id ln_unc_tot ln_unc_com ln_unc_id {
				egen `x'mean = mean(`x')
				egen `x'sd = sd(`x')
				gen z`x' = (`x'-`x'mean) / `x'sd
				drop `x'sd `x'mean
				winsor2 z`x', cuts(1 99) replace
			}
				
		// regressions: bootstrapped standard errors	
			********** why top specification: alpha is a generated variable, and year FE makes sense as a control for global (e.g., geopolitical) uncertainty
			xtset id year
			generate newid = id
			tsset newid year

			foreach x in ln_unc_tot ln_unc_com ln_unc_id {
				qui: xi: bootstrap, rep(200) seed(123) cluster(id) idcluster(newid) nodots nowarn: ///
				ivreg `x' dum_G7j i.year if sample == 1 & unc_tot != . & year < 2020
					eststo
			}
			esttab using "Table_A03_a.rtf", keep(dum*) replace cells(b(star fmt(%9.4f)) t(fmt(%9.2f))) style(fixed) title(Table_A03) ///
			t starlevels(* .10 ** .05 *** .01) b(4) ///
			drop()
			estimates clear

	// monthly
			clear all
			use "data/uncertainty_monthly.dta"
			egen id = group(cou)
			duplicates drop id time, force
			xtset id time

		// dummy for G7 countries (USA, CAN, FRA, DEU, ITA, JPN, GBR)
			// including the U.S.
				gen dum_G7 = (cou == "USA" | cou == "CAN" | cou == "FRA" | cou == "DEU" | cou == "ITA" | cou == "JPN" | cou == "GBR")
			// excluding the U.S.
				gen dum_G7n = (cou == "CAN" | cou == "FRA" | cou == "DEU" | cou == "ITA" | cou == "JPN" | cou == "GBR")
			// excluding Japan
				gen dum_G7j = (cou == "USA" | cou == "CAN" | cou == "FRA" | cou == "DEU" | cou == "ITA" | cou == "GBR")

		// dummy for non-G7 countries (AUS, CHE, ESP, IRL, NLD, NZL, SWE)
				gen dum_nonG7 = (cou == "AUS" | cou == "CHE" | cou == "ESP" | cou == "IRL" | cou == "NLD" | cou == "NZL" | cou == "SWE")

		// dummy for countries in our sample
				gen sample = dum_G7j + dum_nonG7

		keep if sample == 1		
		keep if year < 2020

			// ssc install asdoc
			// help asdoc
			
		/*	
			bys cou : asdoc sum unc_tot, stat(mean p50 sd N) format(%9.4f) replace
			bys cou : asdoc sum unc_id, stat(mean p50 sd N) format(%9.4f) replace
			bys cou : asdoc sum unc_com, stat(mean p50 sd N) format(%9.4f) replace
			bys cou : asdoc sum epu, stat(mean p50 sd N) format(%9.4f) replace

			asdoc tabstat epu, statistics(p50) by(cou) columns(variables)
		*/

		// take logs
			foreach x in unc_tot unc_com unc_id {
				gen ln_`x' = ln(`x')
			}
				

		// standardize and winsorize variables
			foreach x in unc_tot unc_com unc_id {
				egen `x'mean = mean(`x')
				egen `x'sd = sd(`x')
				gen z`x' = (`x'-`x'mean) / `x'sd
				drop `x'sd `x'mean
				winsor2 z`x', cuts(1 99) replace
			}
				
			xtset id time
			generate newid = id
			tsset newid time

		// regressions	
			foreach x in ln_unc_tot ln_unc_com ln_unc_id {
				qui: xi: bootstrap, rep(200) seed(123) cluster(id) idcluster(newid) nodots nowarn: ///
				ivreg `x' dum_G7j i.year if sample == 1 & unc_tot != .
					eststo
			}
			esttab using "Table_A03_b.rtf", keep(dum*) replace cells(b(star fmt(%9.4f)) t(fmt(%9.2f))) style(fixed) title(Table_A03) ///
			t starlevels(* .10 ** .05 *** .01) b(4) ///
			drop()
			estimates clear
			
			
* Table_A04: Productivity and sentiment: Alternative country breakdown
	clear all
	use "data/final_data_set.dta"
	xtset id year
	
	// G7 + Switzerland and Netherlands
		foreach x in f_rtfpna f2_rtfpna f3_rtfpna f4_rtfpna {
				qui: regxfe `x' zcci zd_* zF* if cou == "NLD" | cou == "CHE" | dum_G7 == 1, robust fe(id) cluster(year)
					eststo
			}
		esttab using "Table_A04_a.rtf", replace cells(b(star fmt(%9.4f)) t(fmt(%9.2f))) style(fixed) title(Table_A04) ///
		t starlevels(* .10 ** .05 *** .01) b(4) scalars(r2o)     ///
		drop()
		estimates clear
			
	// Non-G7 without Switzerland and Netherlands		
		foreach x in f_rtfpna f2_rtfpna f3_rtfpna f4_rtfpna {
				qui: regxfe `x' zcci zd_* zF* if cou != "NLD" & cou != "CHE" & dum_G7 == 0, robust fe(id) cluster(year)
					eststo
			}
		esttab using "Table_A04_b.rtf", replace cells(b(star fmt(%9.4f)) t(fmt(%9.2f))) style(fixed) title(Table_A04) ///
		t starlevels(* .10 ** .05 *** .01) b(4) scalars(r2o)     ///
		drop()
		estimates clear

		
* Table_A05. Productivity and sentiment: Extended sample breakdown
	clear all
	use "data/final_data_set_extended.dta"
	xtset id year

	// G7 countries *with* Japan
		foreach x in f_rtfpna f2_rtfpna f3_rtfpna f4_rtfpna {
			qui: regxfe `x' zcci zd_* zF* if dum_G7 == 1, robust fe(id) cluster(year)
				eststo
		}
		esttab using "Table_A05_a.rtf", replace cells(b(star fmt(%9.4f)) t(fmt(%9.2f))) style(fixed) title(Table_A05) ///
		t starlevels(* .10 ** .05 *** .01) b(4) scalars(r2o)     ///
		drop()
		estimates clear
	// *all* non-G7 countries from the OECD
		foreach x in f_rtfpna f2_rtfpna f3_rtfpna f4_rtfpna {
			qui: regxfe `x' zcci zd_* zF* if dum_G7 == 0, robust fe(id) cluster(year)
				eststo
		}
		esttab using "Table_A05_b.rtf", replace cells(b(star fmt(%9.4f)) t(fmt(%9.2f))) style(fixed) title(Table_A05) ///
		t starlevels(* .10 ** .05 *** .01) b(4) scalars(r2o)     ///
		drop()
		estimates clear
	// residual non-G7 countries
		foreach x in f_rtfpna f2_rtfpna f3_rtfpna f4_rtfpna {
			qui: regxfe `x' zcci zd_* zF* if dum_G7 == 0 & dum_nonG7_main == 0, robust fe(id) cluster(year)
				eststo
		}
		esttab using "Table_Table_A05_c.rtf", replace cells(b(star fmt(%9.4f)) t(fmt(%9.2f))) style(fixed) title(Table_A05) ///
		t starlevels(* .10 ** .05 *** .01) b(4) scalars(r2o)     ///
		drop()
		estimates clear

		
* Table_A06: Productivity and sentiment: Alternative FD measures
	clear all
	use "data/final_data_set.dta"
	xtset id year

	// build auxiliary data set
		*	do "do/auxiliary_files/data_construction_FD.do"

	// merge with main data set
		merge m:m country using "data/FD.dta"
		drop _merge
	// correlation between G7 countries and FD
		pwcorr dum_G7 fd1 fd2 fd3 fd4 fd0 if pd != ., sig obs
	// standardize
		************* here we do not subtract the mean because there is no time-series dimension
		foreach x in fd1 fd2 fd3 fd4 fd5 fd6 fd0 {
			egen `x'sd = sd(`x') if pd != .
			gen z`x' = `x' / `x'sd
			drop `x'sd 
		}	
	// create interaction terms
		foreach x in zcci zxb zres l_zpd zl_cci_ort zcci_ort zcci_exp {
			foreach y in fd1 fd2 fd3 fd4 fd5 fd6 fd0 {
				gen `x'_z`y' = `x' * z`y'
			}
		}
	// data availability per country
		tabulate country if zfd0 != .
		tabulate country if zfd1 != . // no observations before 1975
		tabulate country if zfd2 != . // no observations before 1975
		tabulate country if zfd3 != .
		tabulate country if zfd4 != .
		
	/* Here is the correspondence between numbers and types:
			gdp_per_capita_c fd0raw
			mkt_cap_to_gdp_c fd1raw
			stocks_traded_to_gdp_c fd2raw
			domestic_credit_to_gdp_c fd3raw
			bank_credit_to_gdp_c fd4raw
	*/

	// regressions
		foreach x in f_rtfpna f2_rtfpna f3_rtfpna f4_rtfpna {
		foreach z in zfd4 {
			qui: regxfe `x' zcci `z' zcci_`z' zd_* zF*, robust fe(id) cluster(year)
				eststo
		}
		}
		esttab using "Table_A06_a.rtf", replace cells(b(star fmt(%9.4f)) t(fmt(%9.2f))) style(fixed) title(Table_A06) ///
		t starlevels(* .10 ** .05 *** .01) b(4) scalars(r2o)     ///
		drop()
		estimates clear

		foreach x in f_rtfpna f2_rtfpna f3_rtfpna f4_rtfpna {
		foreach z in zfd3 {
			qui: regxfe `x' zcci `z' zcci_`z' zd_* zF*, robust fe(id) cluster(year)
				eststo
		}
		}
		esttab using "Table_A06_b.rtf", replace cells(b(star fmt(%9.4f)) t(fmt(%9.2f))) style(fixed) title(Table_A06) ///
		t starlevels(* .10 ** .05 *** .01) b(4) scalars(r2o)     ///
		drop()
		estimates clear		
		
		
* Table_A07: Productivity and sentiment: FD and quality of institutions
	// import quality of institutions data from Excel to Stata & set time variable
		foreach x in quality_of_govt ///
			{
				clear all
				import excel "orig/`x'.xlsx", sheet("final_series") firstrow
			}

	// merge with main data
		merge m:m cou using "data/final_data_set.dta"
		drop _merge

	// rename and shorten variables	
		rename corrupt q1
		rename b_delayt q2
		rename infraest q3
		rename demo_av q4
		rename taxevas q5
		rename pright q6
		rename legor_uk q7
		rename lgnppcav q8

	// standardize
		foreach x in q1 q2 q3 q4 q5 q6 q7 q8 {
			egen `x'sd = sd(`x') if pd != .
			gen z`x' = `x' / `x'sd
			drop `x'sd 
		}	
		
	// create interaction terms
		foreach x in zcci zxb zres l_zpd zl_cci_ort zcci_ort zcci_exp {
			foreach y in q1 q2 q3 q4 q5 q6 q7 q8 {
				gen `x'_z`y' = `x' * z`y'
			}
		}

	// FD fixed and predetermined
		// merge with main data set
			merge m:m country using "data/FD.dta"
			drop _merge
		// correlation between G7 countries and FD
			pwcorr dum_G7 fd1 fd2 fd3 fd4 fd0 if pd != ., sig obs
		// standardize
			foreach x in fd1 fd2 fd3 fd4 fd5 fd6 fd0 {
				egen `x'sd = sd(`x') if pd != .
				gen z`x' = `x' / `x'sd
				drop `x'sd 
			}	
		// create interaction terms
			foreach x in zcci zxb zres l_zpd zl_cci_ort zcci_ort zcci_exp {
				foreach y in fd1 fd2 fd3 fd4 fd5 fd6 fd0 {
					gen `x'_z`y' = `x' * z`y'
				}
			}
			
	// correlation matrix between FD and quality of institutions
		pwcorr fd0 q1 q2 q3 q4 q5 q6 q7 q8, sig

	// regressions: corruption index
		foreach x in f_rtfpna f2_rtfpna f3_rtfpna f4_rtfpna {
		foreach z in zfd0 {
		foreach y in zq1 {
			qui: regxfe `x' zcci `z' zcci_`z' `y' zcci_`y' zd_* zF*, robust fe(id) cluster(year)
				eststo
		}
		}
		}
		esttab using "Table_A07_a.rtf", keep(zcci*) replace cells(b(star fmt(%9.4f)) t(fmt(%9.2f))) style(fixed) title(Table_A07) ///
		t starlevels(* .10 ** .05 *** .01) b(4) scalars(r2o)     ///
		drop()
		estimates clear

	// regressions: democracy
		foreach x in f_rtfpna f2_rtfpna f3_rtfpna f4_rtfpna {
		foreach z in zfd0 {
		foreach y in zq4 {
			qui: regxfe `x' zcci `z' zcci_`z' `y' zcci_`y' zd_* zF*, robust fe(id) cluster(year)
				eststo
		}
		}
		}
		esttab using "Table_A07_b.rtf", keep(zcci*) replace cells(b(star fmt(%9.4f)) t(fmt(%9.2f))) style(fixed) title(Table_A07) ///
		t starlevels(* .10 ** .05 *** .01) b(4) scalars(r2o)     ///
		drop()
		estimates clear		
		
		
* Table_A08. Investment in R&D		
	clear all
	use "data/final_data_set.dta"
	xtset id year
	generate newid = id
	tsset newid year

	// data availability per country
		tabulate country if g_rd != . & zcci_ort != .
	// data availability sample period
		tabstat year if g_rd != . & zcci_ort != ., statistics(min max)
		
	foreach x in f_g_rd f2_g_rd f3_g_rd f4_g_rd {
			qui: bootstrap, rep(200) seed(123) cluster(id) idcluster(newid) nodots nowarn: ///
			xtreg `x' zcci_ort zcci_exp i.year, fe
				eststo
		}
	esttab using "Table_A08.rtf", replace cells(b(star fmt(%9.4f)) t(fmt(%9.2f))) style(fixed) title(Table_A13) ///
	t starlevels(* .10 ** .05 *** .01) b(4) scalars(r2o)     ///
	drop()
	estimates clear

	
* Table_A09. Capital intensity
	clear all
	use "data/final_data_set.dta"
	xtset id year
	generate newid = id
	tsset newid year

	gen ln_cap_lab = ln(rgcf / emp)
	
	tabstat ln_cap_lab labsh if cci_ort != . & pd != ., statistics(mean sd min p1 p5 p25 p50 p75 p95 p99 max)
	pwcorr ln_cap_lab labsh rtfpna if cci_ort != . & pd != ., sig
	
	// calculate t-step-ahead TFP growth
		foreach x in ln_cap_lab labsh {
			gen f_`x' = f.`x'
			gen f2_`x' = f2.`x'
			gen f3_`x' = f3.`x'
			gen f4_`x' = f4.`x'
		}

	// full sample
		foreach x in f_ln_cap_lab f2_ln_cap_lab f3_ln_cap_lab f4_ln_cap_lab {
			qui: bootstrap, rep(200) seed(123) cluster(id) idcluster(newid) nodots nowarn: xtreg `x' zcci_ort zcci_exp i.year, fe
				eststo
		}
		esttab using "Table_A09_a.rtf", replace cells(b(star fmt(%9.4f)) t(fmt(%9.2f))) style(fixed) title(Table_A12) ///
		t starlevels(* .10 ** .05 *** .01) b(4) scalars(r2)     ///
		drop()
		estimates clear
	// G7 countries
		foreach x in f_ln_cap_lab f2_ln_cap_lab f3_ln_cap_lab f4_ln_cap_lab {
			qui: bootstrap, rep(200) seed(123) cluster(id) idcluster(newid) nodots nowarn: xtreg `x' zcci_ort zcci_exp i.year if dum_G7 == 1, fe
				eststo
		}
		esttab using "Table_A09_b.rtf", replace cells(b(star fmt(%9.4f)) t(fmt(%9.2f))) style(fixed) title(Table_A12) ///
		t starlevels(* .10 ** .05 *** .01) b(4) scalars(r2)     ///
		drop()
		estimates clear
	// non-G7 countries
		foreach x in f_ln_cap_lab f2_ln_cap_lab f3_ln_cap_lab f4_ln_cap_lab {
			qui: bootstrap, rep(200) seed(123) cluster(id) idcluster(newid) nodots nowarn: xtreg `x' zcci_ort zcci_exp i.year if dum_G7 == 0, fe
				eststo
		}
		esttab using "Table_A09_c.rtf", replace cells(b(star fmt(%9.4f)) t(fmt(%9.2f))) style(fixed) title(Table_A12) ///
		t starlevels(* .10 ** .05 *** .01) b(4) scalars(r2)     ///
		drop()
		estimates clear


* Table_A10. Future stock returns, sentiment, and local macro variables
	clear all
	use "data/final_data_set.dta"
	xtset id year

	foreach x in f_ret {
		qui: regxfe `x' zcci zd_*, robust fe(id year) cluster(id)
			eststo
		qui: regxfe `x' zcci zd_* if dum_G7 == 1, robust fe(id year) cluster(id)
			eststo
		qui: regxfe `x' zcci zd_* if dum_G7 == 0, robust fe(id year) cluster(id)
			eststo
	}
	esttab using "Table_A10.rtf", replace cells(b(star fmt(%9.4f)) t(fmt(%9.2f))) style(fixed) title(Table_A09) ///
	t starlevels(* .10 ** .05 *** .01) b(4) scalars(r2)     ///
	drop()
	estimates clear
		
		
* Table_A11. Future stock returns: High v. low sentiment breakdown
	clear all
	use "data/final_data_set.dta"
	xtset id year
	generate newid = id
	tsset newid year

	tabstat zcci_ort if f_ret != ., statistics(med)

	// high sentiment
		foreach x in f_ret f2_ret f3_ret f4_ret {
			qui: bootstrap, rep(200) seed(123) cluster(id) idcluster(newid) nodots nowarn: ///
			xtreg `x' zcci_ort zcci_exp i.year if zcci_ort > 0.0709001, fe
				eststo
		}

		esttab using "Table_A11_a.rtf", keep(zcci_ort zcci_exp) replace cells(b(star fmt(%9.4f)) t(fmt(%9.2f))) style(fixed) title(Table_A04_a) ///
		t starlevels(* .10 ** .05 *** .01) b(4) scalars(r2)     ///
		drop()
		estimates clear
	// low sentiment	
		foreach x in f_ret f2_ret f3_ret f4_ret {
			qui: bootstrap, rep(200) seed(123) cluster(id) idcluster(newid) nodots nowarn: ///
			xtreg `x' zcci_ort zcci_exp i.year if zcci_ort < 0.0709001, fe
				eststo
		}

		esttab using "Table_A11_b.rtf", keep(zcci_ort zcci_exp) replace cells(b(star fmt(%9.4f)) t(fmt(%9.2f))) style(fixed) title(Table_A04_b) ///
		t starlevels(* .10 ** .05 *** .01) b(4) scalars(r2)     ///
		drop()
		estimates clear


* Table_A12. Market efficiency and G10 countries
	// import global factor data	
		clear all
		import excel "orig/3FF_global.xlsx", sheet("annual") firstrow
		destring year, replace
		foreach x in mkt smb hml rf {
			gen ln_`x' = ln(1+`x'/100)
			drop `x'
			rename ln_`x' `x'_glo
		}
		save "data/3FF_global_a.dta", replace

	// import US factor data	
		clear all
		import excel "orig/3FF_USA.xlsx", sheet("annual") firstrow
		destring year, replace
		foreach x in mkt smb hml rf {
			gen ln_`x' = ln(1+`x'/100)
			drop `x'
			rename ln_`x' `x'_usa
		}
		save "data/3FF_USA_a.dta", replace

	// merge data sets	
		clear all
		use "data/final_data_set.dta"
		xtset id year

		merge m:m cou year using "data/returns_data.dta", keep(match master)
		drop _merge
		merge m:m year using "data/3FF_global_a.dta", keep(match master)
		drop _merge
		merge m:m year using "data/3FF_USA_a.dta", keep(match master)
		drop _merge

	// drop individual series
		foreach x in 3FF_global 3FF_USA ///
		{
		erase "data/`x'_a.dta"
		}
		xtset id year

	// equal-weighted
		gen ret = ln(pd)-ln(l.pd)
		
		egen glo_ret_ew = mean(ret), by (year)	
		egen glo_stir_ew = mean(stir), by (year)	
		gen alpha_capm_ew = ret - stir - beta_capm * (glo_ret_ew - glo_stir) // CAPM alpha
		gen alpha_mm_ew = ret - beta_capm * glo_ret_ew  // market model alpha
		gen rp_capm_ew = beta_capm * (glo_ret_ew - glo_stir_ew) // CAPM risk premium
			replace rp_capm_ew = . if alpha_capm_ew == .
		gen rp_mm_ew = beta_capm * glo_ret_ew // market model risk premium
			replace rp_mm_ew = . if alpha_mm_ew == .
			
	// value-weighted
		rename share_prices p

		gen w = p 
		egen sum_w = sum(w), by (year)	
		gen mkt_vw = (w*ret)/sum_w
		gen stir_vw = (w*stir)/sum_w

		egen glo_ret_vw = sum(mkt_vw), by (year)	
		egen glo_stir_vw = sum(stir_vw), by (year)	

		pwcorr glo_ret_vw glo_ret_ew, sig
		pwcorr glo_stir_vw glo_stir_ew, sig

		gen alpha_capm_vw = ret - stir - beta_capm * (glo_ret_vw - glo_stir_vw) // CAPM alpha
		gen alpha_mm_vw = ret - beta_capm * glo_ret_vw // market model alpha
		gen rp_capm_vw = beta_capm * (glo_ret_vw - glo_stir_vw) // CAPM risk premium
			replace rp_capm_vw = . if alpha_capm_vw == .
		gen rp_mm_vw = beta_capm * glo_ret_vw // market model risk premium
			replace rp_mm_vw = . if alpha_mm_vw == .

	// 3FF annual alphas
		gen alpha_capm_glo = ret - stir - b_mkt_glo * mkt_glo
		gen alpha_capm_usa = ret - stir - b_mkt_glo * mkt_usa
		gen alpha_glo = ret - stir - b_mkt_glo * mkt_glo - b_smb_glo * smb_glo - b_hml_glo * hml_glo
		gen alpha_usa = ret - stir - b_mkt_usa * mkt_usa - b_smb_usa * smb_usa - b_hml_usa * hml_usa

	// generate absolute and squared alphas
		foreach x in alpha_capm_ew alpha_capm_vw alpha_mm_ew alpha_mm_vw ///
		alpha_glo alpha_usa alpha_capm_glo alpha_capm_usa alpha_glo_y alpha_usa_y alpha_capm_y /// 
		{
		gen abs_`x' = abs(`x')
		gen sq_`x' = (`x')^2
		}

	// bootstrapped standard errors
		********** why top specification: alpha is a generated variable, and year FE makes no sense in this setup (global mispricing?)
		xtset id year
		generate newid = id
		tsset newid year
		
		foreach x in abs_alpha_capm_ew abs_alpha_usa abs_alpha_glo sq_alpha_capm_ew sq_alpha_usa sq_alpha_glo {
			qui: xi: bootstrap, rep(200) seed(123) cluster(id) idcluster(newid) nodots nowarn: ///
			xtreg `x' dum_G7 if zcci != . & abs_alpha_capm_ew != .
				eststo
		}
		esttab using "Table_A12.rtf", replace cells(b(star fmt(%9.4f)) t(fmt(%9.2f))) style(fixed) title(Table_A11) ///
		t starlevels(* .10 ** .05 *** .01) b(4) ///
		drop()
		estimates clear		
		
		
* Table_A13. Bond returns
	clear all
	use "data/final_data_set.dta"
	xtset id year

		merge m:m cou year using "data/returns_data.dta", keep(match master)
		drop _merge
		xtset id year

	// check coverage
		tabulate country if ltir != . & zcci_ort != .
		tabstat year if ltir != . & zcci_ort != ., statistics(min max)
		tabulate country if stir != . & zcci_ort != .
		tabstat year if stir != . & zcci_ort != ., statistics(min max)
		
	// calculate future values
		foreach x in stir ltir {
			gen f_`x' = f.`x'
			gen f2_`x' = f2.`x'
			gen f3_`x' = f3.`x'
			gen f4_`x' = f4.`x'
			gen f5_`x' = f5.`x'
			gen f6_`x' = f6.`x'
			gen f7_`x' = f7.`x'
			gen f8_`x' = f8.`x'				
		}

	
	generate newid = id
	tsset newid year
	
	// full
		foreach x in f_ltir f2_ltir f3_ltir f4_ltir {
			qui: bootstrap, rep(200) seed(123) cluster(id) idcluster(newid) nodots nowarn: ///
			xtreg `x' zcci_ort zcci_exp i.year, fe
				eststo
		}
		esttab using "Table_A13_a.rtf", keep(zcci*) replace cells(b(star fmt(%9.4f)) t(fmt(%9.2f))) style(fixed) title(Table_A14) ///
		t starlevels(* .10 ** .05 *** .01) b(4) scalars(r2)     ///
		drop()
		estimates clear
	// G7
		foreach x in f_ltir f2_ltir f3_ltir f4_ltir {
			qui: bootstrap, rep(200) seed(123) cluster(id) idcluster(newid) nodots nowarn: ///
			xtreg `x' zcci_ort zcci_exp i.year if dum_G7 == 1, fe
				eststo
		}
		esttab using "Table_A13_b.rtf", keep(zcci*) replace cells(b(star fmt(%9.4f)) t(fmt(%9.2f))) style(fixed) title(Table_A14) ///
		t starlevels(* .10 ** .05 *** .01) b(4) scalars(r2)     ///
		drop()
		estimates clear
	// non-G7
		foreach x in f_ltir f2_ltir f3_ltir f4_ltir {
			qui: bootstrap, rep(200) seed(123) cluster(id) idcluster(newid) nodots nowarn: ///
			xtreg `x' zcci_ort zcci_exp i.year if dum_G7 == 0, fe
				eststo
		}
		esttab using "Table_A13_c.rtf", keep(zcci*) replace cells(b(star fmt(%9.4f)) t(fmt(%9.2f))) style(fixed) title(Table_A14) ///
		t starlevels(* .10 ** .05 *** .01) b(4) scalars(r2)     ///
		drop()
		estimates clear
	
	
* Table_A14. Macroeconomic forecasts
	// import data from Excel to Stata and set time
		foreach x in ltgdp_f g_demand_f g_rgdp_f ///
		{
			clear all
			import excel "orig/OECD/`x'.xlsx", sheet("final_series") firstrow
			destring year, replace
			save "data/`x'_a.dta", replace
		}		
	// merge
		foreach x in ltgdp_f g_demand_f g_rgdp_f ///
		{
			merge m:m cou year using "data/`x'_a.dta", keep(match master)
			drop _merge
		}
	// express growth rates (demand and rgdp forecasts) in decimals	
		foreach x in g_demand_f g_rgdp_f {
			gen `x'100 = `x' / 100
			drop `x'
			rename `x'100 `x'
		}		
			save "data/forecasts_data.dta", replace
	// delete individual series
		foreach x in ltgdp_f g_demand_f g_rgdp_f ///
		{
			erase "data/`x'_a.dta"
		}
		
	clear all
	use "data/final_data_set.dta"
	xtset id year
	merge m:m cou year using "data/forecasts_data.dta", keep(match master)
	drop _merge
	xtset id year

	tabstat g_*, statistics(min mean sd med max)

	// data availability per country
		tabulate country if g_demand_f != . & zcci_ort != .
	// sample period availability
		tabstat year if g_demand_f != . & zcci_ort != ., statistics(min max)
		
	// calculate future values
		foreach x in ltgdp_f g_demand_f g_rgdp_f  {
			gen f_`x' = f.`x'
			gen f2_`x' = f2.`x'
			gen f3_`x' = f3.`x'
			gen f4_`x' = f4.`x'
			gen f5_`x' = f5.`x'
			gen f6_`x' = f6.`x'
			gen f7_`x' = f7.`x'
			gen f8_`x' = f8.`x'				
		}	
	
	generate newid = id
	tsset newid year
	
	// full sample
		foreach x in f_g_demand_f f2_g_demand_f f3_g_demand_f f4_g_demand_f {
				qui: bootstrap, rep(200) seed(123) cluster(id) idcluster(newid) nodots nowarn: ///
				xtreg `x' zcci_ort zcci_exp i.year, fe
					eststo
			}
		esttab using "Table_A14_a.rtf", replace cells(b(star fmt(%9.4f)) t(fmt(%9.2f))) style(fixed) title(Table_forecasts) ///
		t starlevels(* .10 ** .05 *** .01) b(4) scalars(r2)     ///
		drop()
		estimates clear
	// G7 countries
		foreach x in f_g_demand_f f2_g_demand_f f3_g_demand_f f4_g_demand_f {
				qui: bootstrap, rep(200) seed(123) cluster(id) idcluster(newid) nodots nowarn: ///
				xtreg `x' zcci_ort zcci_exp i.year if dum_G7 == 1, fe
					eststo
			}
		esttab using "Table_A14_b.rtf", replace cells(b(star fmt(%9.4f)) t(fmt(%9.2f))) style(fixed) title(Table_forecasts) ///
		t starlevels(* .10 ** .05 *** .01) b(4) scalars(r2)     ///
		drop()
		estimates clear
	// non-G7 countries
		foreach x in f_g_demand_f f2_g_demand_f f3_g_demand_f f4_g_demand_f {
				qui: bootstrap, rep(200) seed(123) cluster(id) idcluster(newid) nodots nowarn: ///
				xtreg `x' zcci_ort zcci_exp i.year if dum_G7 == 0, fe
					eststo
			}
		esttab using "Table_A14_c.rtf", replace cells(b(star fmt(%9.4f)) t(fmt(%9.2f))) style(fixed) title(Table_forecasts) ///
		t starlevels(* .10 ** .05 *** .01) b(4) scalars(r2)     ///
		drop()
		estimates clear	
	
	
* Table_A15. Foreign direct investments
	// import inward FDIs
		// import
			clear all
			import excel "orig/world_bank/FDIs_inflows.xlsx", sheet("final_series") firstrow
		// transform into panel format	
			reshape long ifdi, i(cou) j(year) string	
			sort cou year
			destring year, replace
		// check out distribution
			tabstat ifdi, statistics(min p1 p5 p25 p50 p75 p95 p99 max)
		// winsorize
			winsor2 ifdi, cuts(1 99)
		// take log of *rank* of FDIs
			gen ln_ifdi = ln(1 + ifdi + 3.30e+11)
			tabstat ln_ifdi, statistics(min p1 p5 p25 p50 p75 p95 p99 max)
		// save
			save "data/FDIs_inflows.dta", replace	

	// import outward FDIs
		// import
			clear all
			import excel "orig/world_bank/FDIs_outflows.xlsx", sheet("final_series") firstrow
		// transform into panel format	
			reshape long ofdi, i(cou) j(year) string	
			sort cou year
			destring year, replace
		// check out distribution
			tabstat ofdi, statistics(min p1 p5 p25 p50 p75 p95 p99 max)
		// winsorize
			winsor2 ofdi, cuts(1 99)
		// take log of *rank* of FDIs
			gen ln_ofdi = ln(1 + ofdi + 87.23086)
			tabstat ln_ofdi, statistics(min p1 p5 p25 p50 p75 p95 p99 max)
		// save
			save "data/FDIs_outflows.dta", replace	

	// merge with main data
		clear all
		use "data/final_data_set.dta"
		xtset id year
		foreach x in FDIs_inflows FDIs_outflows ///
		{
			merge m:m cou year using "data/`x'.dta", keep(match master)
			drop _merge
		}
		// delete individual series
			foreach x in FDIs_inflows FDIs_outflows ///
			{
				erase "data/`x'.dta"
			}
			
	// create net inflow
		gen nfdi = ofdi - ifdi
			
	// check coverage
		tabulate country if nfdi != . & zcci_ort != .
		tabstat year if nfdi != . & zcci_ort != ., statistics(min max)
		
	// standardize and winsorize variables
		foreach x in ifdi ofdi nfdi {
			egen `x'mean = mean(`x')
			egen `x'sd = sd(`x')
			gen z`x' = (`x'-`x'mean) / `x'sd
			drop `x'sd `x'mean
			winsor2 z`x', cuts(1 99) replace
		}
			
	tabstat *fdi*, statistics(min p1 p5 p10 p25 p50 p75 p90 p95 p99 max)

	xtset id year

	// calculate future values
		foreach x in zifdi zofdi znfdi {
			gen f_`x' = f.`x'
			gen f2_`x' = f2.`x'
			gen f3_`x' = f3.`x'
			gen f4_`x' = f4.`x'
			gen f5_`x' = f5.`x'
			gen f6_`x' = f6.`x'
			gen f7_`x' = f7.`x'
			gen f8_`x' = f8.`x'				
		}

	generate newid = id
	tsset newid year

	// net FDIs
		// full sample
			foreach x in f_znfdi f2_znfdi f3_znfdi f4_znfdi {
					qui: bootstrap, rep(200) seed(123) cluster(id) idcluster(newid) nodots nowarn: ///
					xtreg `x' zcci_ort zcci_exp i.year, fe
						eststo
				}
			esttab using "Table_A15_a.rtf", replace cells(b(star fmt(%9.4f)) t(fmt(%9.2f))) style(fixed) title(Table_A16) ///
			t starlevels(* .10 ** .05 *** .01) b(4) scalars(r2)     ///
			drop()
			estimates clear
		// G7 countries
			foreach x in f_znfdi f2_znfdi f3_znfdi f4_znfdi {
					qui: bootstrap, rep(200) seed(123) cluster(id) idcluster(newid) nodots nowarn: ///
					xtreg `x' zcci_ort zcci_exp i.year if dum_G7 == 1, fe
						eststo
				}
			esttab using "Table_A15_b.rtf", replace cells(b(star fmt(%9.4f)) t(fmt(%9.2f))) style(fixed) title(Table_A16) ///
			t starlevels(* .10 ** .05 *** .01) b(4) scalars(r2)     ///
			drop()
			estimates clear
		// non-G7 countries
			foreach x in f_znfdi f2_znfdi f3_znfdi f4_znfdi {
					qui: bootstrap, rep(200) seed(123) cluster(id) idcluster(newid) nodots nowarn: ///
					xtreg `x' zcci_ort zcci_exp i.year if dum_G7 == 0, fe
						eststo
				}
			esttab using "Table_A16_c.rtf", replace cells(b(star fmt(%9.4f)) t(fmt(%9.2f))) style(fixed) title(Table_A16) ///
			t starlevels(* .10 ** .05 *** .01) b(4) scalars(r2)     ///
			drop()
			estimates clear	
	
	